
*****************************************************************
*** Program to Produce Table 0 with Bootstrap Standard Errors ***
*****************************************************************
*This table is not presented in the appendix; its too long to present in the text
*and its mainly an intermediate step.
*I computed it for reference

clear
drop _all
set more off

cd "$US_Ineq_Repl"
local Processed = "$US_Ineq_Repl/Processed"
local FFL = "$US_Ineq_Repl/Scripts/FFL"

// customize your file path
cd "`FFL'"
global data "`c(pwd)'\Data"
global table "`c(pwd)'\Tables"

*** define program to run unconditional quantile regression using RIF ***

capture program drop rifuqr	
program define rifuqr
	local qt=`1'		// quantile of interest
	local qc=`qt'/100
	local width=`2'		// kernel density bandwidth
	local expvar `3'	// RIF expvar: call with double quotes
		
	* calculate density
	pctile valx=lrwage3 [aweight=orgwgt], nq(100)	// 100 percentile grids
	kdensity lrwage3 [aweight=orgwgt], at(valx) gen(evalt denst) ///
		width(`width') nograph	// find density at desired grid
	
	* calculate RIF
	gen rif_`qt'=evalt[`qt']+`qc'/denst[`qt'] if lrwage3>=evalt[`qt']  
	replace rif_`qt'=evalt[`qt']-(1-`qc')/denst[`qt'] if lrwage3<evalt[`qt'] 
	drop valx evalt denst
	
	* RIF regression
	reg rif_`qt' `expvar' [aweight=orgwgt]
end

clear	// empty file to store results
save $table\tab0bs.dta , emptyok replace

*** work starts ***
global expvar union pubsect manuf nonwhite female partte married smsa ///
	exper exper2 expe2 expe3 expe4 ///
    educ2 educ3 educ4 educ5 edex ee2 ee3 ee4 ee5 ee6 ee7 ee9 ee10 ee11 ee13 ee14 ee15 ///
    indu2 indu3 indu4 indu5 indu6 indu7 indu8 indu9 indu10 indu11 indu12 indu13 indu14 ///
	indu15 indu16 indu17 indu18 indu19 indu20 ///
	state2 state3 state4 state5 state6 state7 state8 state9 ///
	state10 state11 state12 state13 state14 state15 state16 ///
	state17  state18 state19 state20 state21 state22 state23 ///
	state24 state25 state26 state27 state28 state29 state30 ///
	state31 state32 state33 state34 state35 state36 state37 ///
	state38 state39 state40 state42 state43 state44 state45 ///
	state46 state47 state48 state49 state50 state51
	
foreach t in 86 15 {
	forvalues qt=10(40)90 {
		display  "doing years `t', quantile `qt'"		
	
		use $data\us`t'.dta , clear
			// Table 0 uses lrwage3 
		matrix drop _all

		*** resample and run regressions ***
		
		local reps=500
		local j=1
		while `j'<=`reps' {
			display  "doing bootstrap rep `j'"
			preserve
			if `j'>1 {
				bsample  // use original sample in 1st loop
			}
			quietly: rifuqr `qt' 0.065 "$expvar"
			matrix B=(nullmat(B)\e(b))	// accumulate coefficients
			restore
			local j=`j'+1
		}	// end of bootstrap loop
		clear
		svmat double B, name(b)  // export coefficients

		*** calculate bootstrap standard errors ***
	
		local j=1  // loop over variables in $expvar
		foreach var of varlist b* {
			sum `var'
			gen se`j'=`r(sd)' in 1
			replace se`j'=sqrt(`reps'/(`reps'-1))*se`j' in 1  // adj. degree of freedom
			order se`j', after(`var')  // put s.e right "below" coef
			local j=`j'+1
		}
		keep in 1  // use coef from 1st loop (original sample)
		keep b* se*
		
		xpose, clear varname  //transpose rows to columns
		rename _varname varname
		
		*** format results ***

		// round coef and s.e. by 0.001
		gen y`t'_q`qt'=string(round(v1,0.001))
		
		// add leading zeros
		replace y`t'_q`qt'=regexr(y`t'_q`qt',"^[.]","0.")
		replace y`t'_q`qt'=regexr(y`t'_q`qt',"^-[.]","-0.")

		// add parentheses to s.e.
		replace y`t'_q`qt'="("+y`t'_q`qt'+")" if substr(varname,1,2)=="se"
		
		// calculate p-values and add aesthetics next to coef
		gen pval=2*normal(-abs(v1/v1[_n+1])) if substr(varname,1,1)=="b"
		replace y`t'_q`qt'=y`t'_q`qt'+"*" if pval<=0.1   // * p<=0.1
		replace y`t'_q`qt'=y`t'_q`qt'+"*" if pval<=0.05  // ** p<=0.05
		replace y`t'_q`qt'=y`t'_q`qt'+"*" if pval<=0.01  // *** p<=0.01
		keep varname y`t'_q`qt'
		
		// accumulate results (columns of table)
		merge 1:1 _n using $table\tab0bs.dta , nogen
		save $table\tab0bs.dta , replace		
	}	// end of quantile loop
}	// end of period loop

*** format and export table ***

use $table\tab0bs.dta , clear
order varname y86_q10 y86_q50 y86_q90 y15_q10 y15_q50 y15_q90

// format column titles
label var varname "Variables"
label var y86_q10 "1986 q10"
label var y86_q50 "1986 q50"
label var y86_q90 "1986 q90"
label var y15_q10 "2015 q10"
label var y15_q50 "2015 q50"
label var y15_q90 "2015 q90"

/* format variable labels
	union pubsect manuf nonwhite female partte married smsa ///
	exper exper2 expe2 expe3 expe4 ///
    educ2 educ3 educ4 educ5 edex ee2 ee3 ee4 ee5 ee6 ee7 ee9 ee10 ee11 ee13 ee14 ee15 ///
    indu2 indu3 indu4 indu5 indu6 indu7 indu8 indu9 indu10 indu11 indu12 indu13 indu14 ///
	indu15 indu16 indu17 indu18 indu19 indu20 ///
	state2 state3 state4 state5 state6 state7 state8 state9 ///
	state10 state11 state12 state13 state14 state15 state16 ///
	state17  state18 state19 state20 state21 state22 state23 ///
	state24 state25 state26 state27 state28 state29 state30 ///
	state31 state32 state33 state34 state35 state36 state37 ///
	state38 state39 state40 state42 state43 state44 state45 ///
	state46 state47 state48 state49 state50 state51
*/

replace varname="" if substr(varname,1,2)=="se"
replace varname="Unionization" if varname=="b1"
replace varname="Public Sector" if varname=="b2"
replace varname="Manufacturing Sector" if varname=="b3"
replace varname="Nonwhite" if varname=="b4"
replace varname="Women" if varname=="b5"
replace varname="Part Time" if varname=="b6"
replace varname="Married" if varname=="b7"
replace varname="Urban" if varname=="b8"
replace varname="Experience" if varname=="b9"
replace varname="Experience sq." if varname=="b10"
replace varname="expe2" if varname=="b11"
replace varname="expe3" if varname=="b12"
replace varname="expe4" if varname=="b13"
replace varname="High School" if varname=="b14"
replace varname="Some College" if varname=="b15"
replace varname="Associate Degree" if varname=="b16"
replace varname="College Degree" if varname=="b17"
replace varname="edex" if varname=="b18"
replace varname="ee2" if varname=="b19"
replace varname="ee3" if varname=="b20"
replace varname="ee4" if varname=="b21"
replace varname="ee5" if varname=="b22"
replace varname="ee6" if varname=="b23"
replace varname="ee7" if varname=="b24"
replace varname="ee8" if varname=="b25"
replace varname="ee9" if varname=="b26"
replace varname="ee10" if varname=="b27"
replace varname="ee11" if varname=="b28"
replace varname="ee12" if varname=="b29"
replace varname="ee13" if varname=="b30"
replace varname="ee14" if varname=="b31"
replace varname="ee15" if varname=="b32"
replace varname="indu2" if varname=="b33"
replace varname="indu3" if varname=="b34"
replace varname="indu4" if varname=="b35"
replace varname="indu5" if varname=="b36"
replace varname="indu6" if varname=="b37"
replace varname="indu7" if varname=="b38"
replace varname="indu8" if varname=="b39"
replace varname="indu9" if varname=="b40"
replace varname="indu10" if varname=="b41"
replace varname="indu11" if varname=="b42"
replace varname="indu12" if varname=="b43"
replace varname="indu13" if varname=="b44"
replace varname="indu14" if varname=="b45"
replace varname="indu15" if varname=="b46"
replace varname="indu16" if varname=="b47"
replace varname="indu17" if varname=="b48"
replace varname="indu18" if varname=="b49"
replace varname="indu19" if varname=="b50"
replace varname="indu20" if varname=="b51"

// export table
export excel using "$table\tab0bs.xls", replace first(varlabel)
*rm $table\tab1bs.dta


